package com.samphanie.ane.core.security.handler;

import com.samphanie.ane.core.security.utils.SecurityCoreConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author ZSY
 * @createTime 2021/3/13 17:51
 */
@Slf4j
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        if (authentication != null && authentication.getDetails() != null) {
            request.getSession().invalidate();
        }

        response.setStatus(HttpStatus.OK.value());
        response.getWriter().println();
        log.debug("成功退出登录");
        response.sendRedirect(SecurityCoreConstants.DEFAULT_LOGIN_PAGE);
    }
}
